/*
 * Licensed under the MIT License
 * 
 * Copyright (c) 2011 Kay Kreuning
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * http://code.google.com/p/hyves-as3-sdk/
 */
package nl.hyves.api.login
{
	import com.adobe.cairngorm.contract.Contract;
	import nl.hyves.api.net.IHyvesResponse;
	import nl.hyves.api.core.IHyvesAPI;
	import nl.hyves.api.core.OAuthToken;
	import nl.hyves.api.methods.IMethod;
	import nl.hyves.api.utils.AuthUtil;

	internal class AbstractLogin implements ILogin
	{
		protected static const AUTHORIZE_ENDPOINT:String = 'http://www.hyves.nl/api/authorize/';
		
		private var method:IMethod;
		private var onAuthenticated:Function;
		private var apiMethods:Array;
		private var callback:Function;
		
		protected var callbackURL:String;
		protected var service:IHyvesAPI;
		protected var requesttoken:OAuthToken;

		public function AbstractLogin(method:IMethod, onAuthenticated:Function)
		{
			Contract.precondition(method != null);
			Contract.precondition(onAuthenticated != null);
			
			if (method.parameters.methods)
			{
				apiMethods = method.parameters.methods.split(',');
			}

			this.method = method;
			this.onAuthenticated = onAuthenticated;
			
			Contract.postcondition(this.method != null);
			Contract.postcondition(this.onAuthenticated != null);
		}

		public function authenticate(service:IHyvesAPI, callback:Function):void
		{
			Contract.precondition(service != null);
			Contract.precondition(callback != null);
			
			this.service = service;
			this.callback = callback;
			
			Contract.precondition(this.service != null);
			Contract.precondition(this.callback != null);

			service.call(method, onAuthenticated, {api_immediate: true});
		}

		protected function onRequesttoken(response:IHyvesResponse):void
		{
			Contract.precondition(response && response.raw);
			
			requesttoken = AuthUtil.extractToken(response.data);
			
			Contract.postcondition(requesttoken != null);
			
			callback(requesttoken);
		}

		protected function onAccesstoken(response:IHyvesResponse):void
		{
			Contract.precondition(response && response.raw);
			
			var accesstoken:OAuthToken = AuthUtil.extractToken(response.data);

			if (apiMethods && !accesstoken.methods)
			{
				accesstoken.methods = apiMethods.join(',');
			}

			Contract.postcondition(accesstoken != null);
			
			callback(accesstoken);
		}
		
		protected function buildAuthorizeURL(token:String, callback:String = ''):String
		{
			Contract.precondition(token && token != '');
			
			var url:String = AUTHORIZE_ENDPOINT + '?oauth_token=' + encodeURI(token);
			
			if (callback)
			{
				url += '&oauth_callback=' + encodeURIComponent(callback);
			}
			
			Contract.postcondition(url && url != '');
			
			return url;
		}
	}
}
